﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using YT.Commons;
using YT.DB;

namespace YT.Modules
{
    public partial class frmLookUp : Form
    {
        public bool mblnHaveReturnValue = false;
        public string strKeyValue;
        private TitleType FromType;
        public frmLookUp()
        {
            InitializeComponent();
        }

        public frmLookUp( TitleType fromType)
        {            
           
            InitializeComponent();
            FromType = fromType;
        }

        private string GetType(TitleType fromType)
        {
            if (fromType == TitleType.NOSE)
            {
                return "NOSE";
            }
            else if (fromType == TitleType.Dizziness)
            {
                return "Dizziness";
            }
            else if (fromType == TitleType.MeniereDisease)
            {
                return "MeniereDisease";
            }
            else if (fromType == TitleType.IgE)
            {
                return "IgE";
            }
            else
                return "";
        }

        private void FillGrid()
        {
            string strTypeDesc = GetType(FromType);
            this.dgDtl.Rows.Clear();
            string strSQL="select * from mSickPerson where Type='"+ GetType( FromType )  +"'";
            if (txtSickName.Text != "") strSQL += " and sickname like '%" + txtSickName.Text + "%'";
            DBAccess dbcon = new DBAccess(false, false);
            DataTable dtTable = dbcon.GetDataTable(strSQL);
            foreach (DataRow drRow in dtTable.Rows)
            {
                this.dgDtl.Rows.Add();
                this.dgDtl.Rows[this.dgDtl.Rows.Count - 1].Cells[colRecID.Index ].Value = CvtFunc.ToString(drRow["RecID"]);
                this.dgDtl.Rows[this.dgDtl.Rows.Count - 1].Cells[ColSickRecID.Index].Value = CvtFunc.ToString(drRow["SickRecID"]);
                this.dgDtl.Rows[this.dgDtl.Rows.Count - 1].Cells[ColSickName.Index].Value = CvtFunc.ToString(drRow["SickName"]);
                this.dgDtl.Rows[this.dgDtl.Rows.Count - 1].Cells[colPhone.Index].Value = CvtFunc.ToString(drRow["MobilePhone"]);

            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            mblnHaveReturnValue = true;
            this.Hide(); 
        }

        public  string GetReturnValue()
        {
            this.dgDtl.EndEdit();
            string strReturnValue = "";
            if (mblnHaveReturnValue)
            {
                foreach (DataGridViewRow drSel in this.dgDtl.Rows)
                {
                    if (Convert.ToBoolean(drSel.Cells[colSel.Index].Value))
                    {
                        strReturnValue += "," + drSel.Cells[colPhone.Index].Value.ToString().Trim();
                    }
                }
                if (strReturnValue.Length > 0) strReturnValue = strReturnValue.Substring(1, strReturnValue.Length - 1);
            }
            return strReturnValue;
        }

        private void frmLookUp_Load(object sender, EventArgs e)
        {
            FillGrid();
        }

        private void btnSelectAll_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow drSel in this.dgDtl.Rows)
            {
                drSel.Cells[colSel.Index].Value = true;
            }
        }

        private void btnUnselectAll_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow drSel in this.dgDtl.Rows)
            {
                drSel.Cells[colSel.Index].Value = false ;
            }
        }

        private void txtSickName_TextChanged(object sender, EventArgs e)
        {
            
            FillGrid();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Hide(); 
        }

     


    }
}
